package dao;

import dao.base.BaseDao;
import model.Dept;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;
import java.util.Map;

public class DeptDao extends BaseDao<Dept> {
    public DeptDao(Class aClass) {
        super(Dept.class);
    }

    /**
     * 查找全部部分信息
     * @return List<Map<String,Object>>
     */
    public List<Map<String,Object>> findDept(){
        String sql = "select d.id as deptId,d.name as deptName, u.id as uId,u.username,u.real_name from" +
                " dept d left join user u on d.id = u.dept_id";
        return getJdbcTemplate().queryForList(sql);
    }

    public Dept findDeptByName(String name){
        String sql = "select * from dept where name = ?";
        Dept dept = getJdbcTemplate().queryForObject(sql, new BeanPropertyRowMapper<>(Dept.class), name);
        return dept;
    }

    public List<Map<String, Object>> findUserByDeptId(Integer deptId) {

        return getJdbcTemplate().queryForList( "select id,real_name realName,is_secret isSecret from user where dept_id=?", deptId);
    }
}
